﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

using Buddy.BehaviorTree;
using Buddy.Common;
using Buddy.CommonBot;
using Buddy.Common.Plugins;
using Buddy.Swtor;
using Buddy.Swtor.Objects;

using WingIt.Dynamics;
using Action = Buddy.BehaviorTree.Action;

namespace Gather
{
    public class Scavenger : IPlugin
    {
        #region Implementation of IEquatable<IPlugin>

        public bool Equals(IPlugin other)
        {
            return other.Name == Name;
        }

        #endregion

        #region Implementation of IPlugin

        public string Author { get { return "xsol"; } }

        public Version Version { get { return new Version(1, 0); } }

        public string Name { get { return "Scavenger"; } }

        public string Description { get { return "Attempts to use bioanalysis nodes."; } }

        public Window DisplayWindow { get { return null; } }

        public TorPlaceable Node
        {
            get
            {
                return ObjectManager.GetObjects<TorPlaceable>().FirstOrDefault(
                    o =>
                        //o.Name.Contains("") || 
                        o.Name.Contains("Desh Junk Pile") ||
                        o.Name.Contains("Aluminium Salvaged Heap") ||
                        o.Name.Contains("Bronzium Wreckage") ||
                        o.Name.Contains("Chanlon Vehicle Debris") ||
                        o.Name.Contains("Phobium Barrel") ||
                        o.Name.Contains("Bondite Jetsam") ||
                        o.Name.Contains("Diatium Cargo") ||
                        o.Name.Contains("Electrum Cache") ||
                        o.Name.Contains("Neutronium Speeder Parts") ||
                        o.Name.Contains("Durasteel Shipwreck")
                );
            }
        }

        private DateTime LastChecked { get; set; }

        private int count { get; set; }

        public void OnInitialize() { }

        private void Log(string message)
        {
            Logging.WriteDiagnostic("[Scavenger] " + message);
        }

        /// <summary> Executes the pulse action. This is called every "tick" of the bot. </summary>
        public void OnPulse()
        {
            if (BuddyTor.Me.InCombat)
                return;

            Log("Scavenger - Running");
            if (DateTime.Now.Subtract(LastChecked).TotalSeconds > 10)
            {
                LastChecked = DateTime.Now;
                TreeHooks.Instance.InsertHook("TreeStart", 0,
                new PrioritySelector(context => Node,
                    new Decorator(ret => ret != null && !BuddyTor.Me.InCombat,
                        new PrioritySelector(
                            CommonBehaviors.MoveAndStop(ret => (ret as TorPlaceable).Position, 0.4f, true, "[Scavenger] Move to Node"),
                            new Sequence(
                                new Action(ret => (ret as TorPlaceable).Interact()),
                                new Action(ret => count++),
                                new Action(ret => Logging.Write("[Scavenger] Interacting with " + (ret as TorObject).Name + "(" + count + ")")),
                                new WaitContinue(4, ret => Node.IsLootable, null)
                                )
                            )
                        )
                    )

                );
            }
        }

        /// <summary> Executes the initialize action. This is called at initial bot startup. (When the bot itself is started, not when Start() is called) </summary>


        /// <summary> Executes the shutdown action. This is called when the bot is shutting down. (Not when Stop() is called) </summary>
        public void OnShutdown()
        {
        }

        /// <summary> Executes the enabled action. This is called when the user has enabled this specific plugin via the GUI. </summary>
        public void OnEnabled()
        {
            Logging.Write("[Scavenger] Enabled");
        }

        /// <summary> Executes the disabled action. This is called whent he user has disabled this specific plugin via the GUI. </summary>
        public void OnDisabled()
        {
            Logging.Write("[Scavenger] Disabled");
        }

        #endregion
    }
}
